package com.alwvfhp.tools.Powershell.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
 * 
 * @author sujian
 * 
 */
public class FileUtil {
	/**
	 * 读数据
	 * @param jfile
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public String readData(File jfile,String encoding)throws Exception{
		try (BufferedReader br=new BufferedReader(
				new InputStreamReader(
						new FileInputStream(jfile),encoding))){
			StringBuffer buf = new StringBuffer();
			String line=null;
			while((line=br.readLine())!=null){
				buf.append(line);
			}
			return new String(buf);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
	}
	/**
	 * 写数据
	 * @param jfile
	 * @param encoding
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public boolean writeData(File jfile,String encoding,Object...data)throws Exception{
		try(BufferedWriter bw = new BufferedWriter(
				new OutputStreamWriter(
						new FileOutputStream(jfile),encoding))) {
			for(Object s:data){
				bw.write(s+"\n\r");
			}
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
	}
}
